package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RadialGradient
import android.graphics.Shader
import android.graphics.SweepGradient
import android.util.AttributeSet
import android.view.View
import com.android.kotlincoroutinemvvmdemo.R

/*
* ColorGradientView5
* */
class ColorGradientView5 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private var mGradient: Shader? = null

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
        }
    }

    init {
        setLayerType(View.LAYER_TYPE_HARDWARE, null)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))

        if (mGradient == null) {
            mGradient = RadialGradient(
                measuredWidth / 2F,
                measuredHeight / 2F,
                measuredHeight * 2F,
                intArrayOf(
                    resources.getColor(R.color.color_03FADD),
                    resources.getColor(R.color.color_F13376),
                    resources.getColor(R.color.color_AA9721),
                ),
                floatArrayOf(
                    0.1F,
                    0.2F,
                    0.5F
                ),
                Shader.TileMode.CLAMP
            )
        }
        mPaint0.shader = mGradient

        canvas?.apply {
            drawRect(
                0F,
                0F,
                measuredWidth.toFloat(),
                measuredHeight.toFloat(),
                mPaint0
            )
        }
    }
}